package com.phantomjs;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.omg.CORBA.IMP_LIMIT;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.WrapsDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class WeixinTest {
	private static final String ua="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
	public static void main(String[] args) throws Exception {
		
		// 加载驱动
//		System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
//		// 设置useragent
//		String useragent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0";
//		FirefoxProfile profile = new FirefoxProfile();
//		profile.setPreference("general.useragent.override", useragent);
//		WebDriver driver = new FirefoxDriver(profile);
		
		DesiredCapabilities caps = new DesiredCapabilities();
        caps.setJavascriptEnabled(true);  
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
		WebDriver driver = new PhantomJSDriver(caps);
		
//		String PROXY = "93.171.155.14:8080";
//		org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
//		proxy.setHttpProxy(PROXY)
//		     .setFtpProxy(PROXY)
//		     .setSslProxy(PROXY);
//		DesiredCapabilities cap = new DesiredCapabilities();
//		cap.setCapability(CapabilityType.PROXY, proxy);
//		WebDriver driver = new PhantomJSDriver(cap);
		
		
//		String url="http://weixin.sogou.com/";
		String url="http://weixin.sogou.com/weixin?query=%E5%86%9C%E5%AE%B6%E4%B9%90&type=2&page=4&ie=utf8";
		driver.get(url);
		
//		try {
//			Thread.sleep(1000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//		driver.navigate().refresh(); 
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		valid(driver);
		
		valid(driver);
		
		System.out.println(driver.getPageSource());
		
//		driver.quit();
	}
	
	static String IMG_TMP="http://weixin.sogou.com/antispider/util/seccode.php?tc=AA ";
	private static void valid(WebDriver driver) throws IOException, MalformedURLException {
		String pageSource = driver.getPageSource();
//		System.out.println(pageSource);
		if(pageSource.contains("您的访问出错了")){
			
			WebElement submit = driver.findElement(By.id("submit"));
			submit.click();
			
			WebElement img = driver.findElement(By.xpath("//*[@id='seccodeImage']"));
			
			try {
				File imgFile = captureElement(img);
				FileUtils.copyFile(imgFile, new File("D:/vcode.jpg"));
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			System.out.println("截图成功。。。。。");
			
			
			
			String key = "bbfe5b36615b9d3342771d2d7832a914";
			String uname = "dmcjh";
			String upwd = "dmcjh123";
			int idApp = 45909;
			Dama2Web web = new Dama2Web(idApp, key, uname, upwd);
			 
		   //编码
		   byte[] br = FileUtils.readFileToByteArray( new File("D:/vcode.jpg"));
		   //根据图片数据解码
		   
		   String result= web.decodeAndGetResult(44, 5000, br).result;
		   
		   System.out.println(result);
			
			WebElement input = driver.findElement(By.id("seccodeInput"));
			input.sendKeys(result);
			
			
			submit.click();
			
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	//页面元素截图  
    public static File captureElement(WebElement element) throws Exception {  
        WrapsDriver wrapsDriver = (WrapsDriver) element;  
        // 截图整个页面  
        File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);  
        BufferedImage img = ImageIO.read(screen);  
        // 获得元素的高度和宽度  
        int width = element.getSize().getWidth();  
        int height = element.getSize().getHeight();  
        // 创建一个矩形使用上面的高度，和宽度  
        Rectangle rect = new Rectangle(width, height);  
        // 得到元素的坐标  
        Point p = element.getLocation();  
        BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,rect.height);  
        //存为png格式  
        ImageIO.write(dest, "png", screen);  
        return screen;  
    }  
}
